Trò chơi Angry Birds trong UNITY Engine
31.688 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4
5 /// <summary>
6 /// Found in
7 /// http://unity3d.com/pt/learn/tutorials/modules/beginner/platform-specific/pinch-zoom
8 /// Contains both perspective and orthographic stuff, in this 2D game we'll
9 /// be using only the orthographic one
10 /// </summary>
11 public class CameraPinchToZoom : MonoBehaviour
12 {
13 public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
14 public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.
15
16
17 void Update()
18 {
19 // If there are two touches on the device...
20 if (Input.touchCount == 2)
21 {
22 // Store both touches.
23 Touch touchZero = Input.GetTouch(0);
24 Touch touchOne = Input.GetTouch(1);
25
26 // Find the position in the previous frame of each touch.
27 Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
28 Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
29
30 // Find the magnitude of the vector (the distance) between the touches in each frame.
31 float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
32 float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
33
34 // Find the difference in the distances between each frame.
35 float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
36
37 // If the camera is orthographic...
38 if (GetComponent<Camera>().orthographic)
39 {
40 // ... change the orthographic size based on the change in distance between the touches.
41 GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
42
43 // Make sure the orthographic size never drops below zero.
44 GetComponent<Camera>().orthographicSize = Mathf.Clamp(GetComponent<Camera>().orthographicSize, 3f, 5f);
45 }
46 else //perspective
47 {
48 // Otherwise change the field of view based on the change in distance between the touches.
49 GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
50
51 // Clamp the field of view to make sure it's between 0 and 180.
52 GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 0.1f, 179.9f);
53 }
54 }
55 }
56 }
2 using System.Collections;
3
4
5 /// <summary>
6 /// Found in
7 /// http://unity3d.com/pt/learn/tutorials/modules/beginner/platform-specific/pinch-zoom
8 /// Contains both perspective and orthographic stuff, in this 2D game we'll
9 /// be using only the orthographic one
10 /// </summary>
11 public class CameraPinchToZoom : MonoBehaviour
12 {
13 public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
14 public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.
15
16
17 void Update()
18 {
19 // If there are two touches on the device...
20 if (Input.touchCount == 2)
21 {
22 // Store both touches.
23 Touch touchZero = Input.GetTouch(0);
24 Touch touchOne = Input.GetTouch(1);
25
26 // Find the position in the previous frame of each touch.
27 Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
28 Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
29
30 // Find the magnitude of the vector (the distance) between the touches in each frame.
31 float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
32 float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
33
34 // Find the difference in the distances between each frame.
35 float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
36
37 // If the camera is orthographic...
38 if (GetComponent<Camera>().orthographic)
39 {
40 // ... change the orthographic size based on the change in distance between the touches.
41 GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
42
43 // Make sure the orthographic size never drops below zero.
44 GetComponent<Camera>().orthographicSize = Mathf.Clamp(GetComponent<Camera>().orthographicSize, 3f, 5f);
45 }
46 else //perspective
47 {
48 // Otherwise change the field of view based on the change in distance between the touches.
49 GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
50
51 // Clamp the field of view to make sure it's between 0 and 180.
52 GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 0.1f, 179.9f);
53 }
54 }
55 }
56 }